//
//  ArticleBottomViewCell.m
//  yigou
//
//  Created by wangdianlei on 2017/1/18.
//  Copyright © 2017年 hanyiwang. All rights reserved.
//

#import "ArticleBottomViewCell.h"
#import "ArticleModel.h"
#import "PublishArticleViewController.h"
#import "MyAddTribeListViewController.h"

@implementation ArticleBottomViewCell

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MSGSELECTTRIBE object:nil];
}

- (void)choosetribe:(NSNotification *)notify {
    if ([PublishData sharedPublishData].chooseTribeName.length > 0) {
        self.tribename.text = [PublishData sharedPublishData].chooseTribeName;
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(choosetribe:) name:MSGSELECTTRIBE object:nil];
    
    CGFloat interw = (ScreenWidth - 24 - 50  * 6) / 5;
    if (interw < 0)
        interw = 0;
    // Initialization code
    self.button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    self.button1.frame = CGRectMake(12, 10, 50, 50);
    [self.button1 setTitle:@"图片" forState:UIControlStateNormal];
    self.button1.titleLabel.font = [UIFont systemFontOfSize:14];
    self.button1.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [self.button1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.button1 setImage:[UIImage imageNamed:@"pubaddimg"] forState:UIControlStateNormal];
    [self.button1 setTitleEdgeInsets:UIEdgeInsetsMake(24, -15, 0, 0)];
    [self.button1 setImageEdgeInsets:UIEdgeInsetsMake(0, 15, 20, 0)];
    
    self.button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    self.button2.frame = CGRectMake(12 + interw + 50, 10, 50, 50);
    [self.button2 setTitle:@"拍照" forState:UIControlStateNormal];
    self.button2.titleLabel.font = [UIFont systemFontOfSize:14];
    self.button2.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [self.button2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.button2 setImage:[UIImage imageNamed:@"pubaddcam"] forState:UIControlStateNormal];
    [self.button2 setTitleEdgeInsets:UIEdgeInsetsMake(24, -15, 0, 0)];
    [self.button2 setImageEdgeInsets:UIEdgeInsetsMake(0, 15, 20, 0)];
    
    self.button3 = [UIButton buttonWithType:UIButtonTypeCustom];
    self.button3.frame = CGRectMake(12 + (interw + 50) * 2, 10, 50, 50);
    [self.button3 setTitle:@"文字" forState:UIControlStateNormal];
    self.button3.titleLabel.font = [UIFont systemFontOfSize:14];
    self.button3.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [self.button3 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.button3 setImage:[UIImage imageNamed:@"pubaddtext"] forState:UIControlStateNormal];
    [self.button3 setTitleEdgeInsets:UIEdgeInsetsMake(24, -15, 0, 0)];
    [self.button3 setImageEdgeInsets:UIEdgeInsetsMake(0, 15, 20, 0)];
    
    [self.contentView addSubview:self.button1];
    [self.contentView addSubview:self.button2];
    [self.contentView addSubview:self.button3];
    
    [self.button1 addTarget:self action:@selector(addImg) forControlEvents:UIControlEventTouchUpInside];
    [self.button2 addTarget:self action:@selector(addCam) forControlEvents:UIControlEventTouchUpInside];
    [self.button3 addTarget:self action:@selector(addText) forControlEvents:UIControlEventTouchUpInside];
    [self addChooseTribe];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)addChooseTribe {
    UIView *bg = [[UIView alloc] init];
    [self.contentView addSubview:bg];
    [bg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.mas_left).offset(12);
        make.right.equalTo(self.contentView.mas_right).offset(-12);
        make.height.mas_equalTo(50);
        make.bottom.equalTo(self.contentView.mas_bottom);
    }];
    bg.backgroundColor = [UIColor colorWithHexString:@"E9E9F1"];
    
    bg.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickChooseTribe)];
    tap.numberOfTapsRequired = 1;
    [bg addGestureRecognizer:tap];
    
    UIView *line1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth - 24, 1)];
    line1.backgroundColor = LineColor;
    [bg addSubview:line1];
    
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(12, 1, 100, 48)];
    title.text = @"选择部落";
    title.font = [FontTool fontWithSize:15];
    title.textColor = [UIColor blackColor];
    title.textAlignment = NSTextAlignmentLeft;
    [bg addSubview:title];
    
    self.tribename = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth - 30 - 24 - 5 - 100, 1, 100, 48)];
    self.tribename.text = @"";
    self.tribename.font = [FontTool fontWithSize:15];
    self.tribename.textColor = [UIColor blackColor];
    self.tribename.textAlignment = NSTextAlignmentRight;
    [bg addSubview:self.tribename];
    
    UIImageView *arrow = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenWidth - 30 - 24, 16, 18, 18)];
    arrow.image = [UIImage imageNamed:@"rightarrow"];
    [bg addSubview:arrow];
    
    UIView *line2 = [[UIView alloc] initWithFrame:CGRectMake(0, 49, ScreenWidth - 24, 1)];
    line2.backgroundColor = LineColor;
    [bg addSubview:line2];
}

- (void)clickChooseTribe {
    MyAddTribeListViewController *vc = [[MyAddTribeListViewController alloc] initWithNibName:@"MyAddTribeListViewController" bundle:nil];
    vc.type = IMyAddTribeListChoose;
    if (self.controller) {
        [self.controller.view endEditing:YES];
        [self.controller.navigationController pushViewController:vc animated:YES];
    }
}

- (void)addImg {
    NSInteger maxCount = 1;
    if (ISIOS7 || ISIOS8_0) {
        ELCImagePickerController *elcPicker = [ELCImagePickerController new];
        elcPicker.maximumImagesCount = maxCount;
        elcPicker.imagePickerDelegate = self;
        
        if (self.controller) {
            [self.controller presentViewController:elcPicker animated:YES completion:nil];
        }
    } else {
        QBImagePickerController *imagePickerController = [QBImagePickerController new];
        imagePickerController.assetCollectionSubtypes =@[
                                                         @(PHAssetCollectionSubtypeSmartAlbumUserLibrary), //相机胶卷
                                                         @(PHAssetCollectionSubtypeAlbumMyPhotoStream), //我的照片流
                                                         @(PHAssetCollectionSubtypeSmartAlbumPanoramas), //全景图
                                                         @(PHAssetCollectionSubtypeSmartAlbumBursts), //连拍模式拍摄的照片
                                                         @(PHAssetCollectionSubtypeSmartAlbumFavorites)
                                                         ];
        imagePickerController.delegate = self;
        imagePickerController.allowsMultipleSelection = YES;
        imagePickerController.mediaType = QBImagePickerMediaTypeImage;
        imagePickerController.maximumNumberOfSelection = maxCount;
        imagePickerController.showsNumberOfSelectedAssets = YES;
        if (self.controller) {
            [self.controller presentViewController:imagePickerController animated:YES completion:nil];
        }
    }
}

- (void)addCam {
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
    
    if (![UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
        sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    }
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.sourceType = sourceType;
    
    if (self.controller) {
        [self.controller presentViewController:picker animated:YES completion:^{
        }];
    }
}

- (void)addText {
    ArticleModel *baseone = [ArticleModel new];
    baseone.type = ArticleModelTxt;
    baseone.txt = @"";
    [[PublishData sharedPublishData].localArray addObject:baseone];
    if (self.controller) {
        PublishArticleViewController *pb = (PublishArticleViewController *)self.controller;
        [pb refreshTable];
    }
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
    [picker dismissViewControllerAnimated:YES completion:^{
        ArticleModel *baseone = [ArticleModel new];
        baseone.type = ArticleModelImageTxt;
        baseone.txt = @"";
        baseone.img = image;
        [[PublishData sharedPublishData].localArray addObject:baseone];
        
        if (self.controller) {
            PublishArticleViewController *pb = (PublishArticleViewController *)self.controller;
            [pb refreshTable];
        }
    }];
}

- (void)elcImagePickerController:(ELCImagePickerController *)picker didFinishPickingMediaWithInfo:(NSArray *)info
{
    //TODO:changeToData
    [picker dismissViewControllerAnimated:YES completion:^{
        for (int i = 0;i<[info count];i++) {
            UIImage *img = [[info objectAtIndex:i]objectForKey:@"UIImagePickerControllerOriginalImage"];
            ArticleModel *baseone = [ArticleModel new];
            baseone.type = ArticleModelImageTxt;
            baseone.txt = @"";
            baseone.img = img;
            [[PublishData sharedPublishData].localArray addObject:baseone];
            if (self.controller) {
                PublishArticleViewController *pb = (PublishArticleViewController *)self.controller;
                [pb refreshTable];
            }
        }
    }];
}

- (void)elcImagePickerControllerDidCancel:(ELCImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:^{
        DYLog(@"cancl");
    }];
}

- (void)qb_imagePickerController:(QBImagePickerController *)imagePickerController didFinishPickingAssets:(NSArray *)assets {
    NSUInteger count = assets.count;
    __block NSUInteger select = 0;
    PHImageRequestOptions *option = [[PHImageRequestOptions alloc] init];
    option.synchronous = YES;
    for (PHAsset *asset in assets) {
        [[PHCachingImageManager defaultManager]requestImageDataForAsset:asset options:option resultHandler:^(NSData * imageData, NSString * dataUTI, UIImageOrientation orientation, NSDictionary * info) {
            if(imageData) {                
                ArticleModel *baseone = [ArticleModel new];
                baseone.type = ArticleModelImageTxt;
                baseone.txt = @"";
                baseone.img = [UIImage imageWithData:imageData];
                [[PublishData sharedPublishData].localArray addObject:baseone];
                if (self.controller) {
                    PublishArticleViewController *pb = (PublishArticleViewController *)self.controller;
                    [pb refreshTable];
                }
                select++;
            }
            if(select == count) {
                [imagePickerController dismissViewControllerAnimated:YES completion:NULL];
                return;
            }
        }];
        
    }
}

- (void)qb_imagePickerControllerDidCancel:(QBImagePickerController *)imagePickerController {
    [imagePickerController dismissViewControllerAnimated:NO completion:NULL];
}

@end
